var cssHide = "hide";

function load() {

	setEvents();
	callServiceHotBook();
    CartFunction.setText();
	showPage("content-index");
	//pagesManager.setPage(Pages.home, false);
	ajax.get({
		url : "http://localhost:8080/book-service/public/book/authen/info",
		success : function(data, xhr) {
			console.log(data);

			$id("login").addClass(cssHide);
			$id("profile").removeClass(cssHide);
			$id("panel").addClass(cssHide);

			var userName = data.getElementsByTagName("userName")[0].childNodes[0].nodeValue;
			var avatar = data.getElementsByTagName("avatar")[0].childNodes[0].nodeValue;
			var email = data.getElementsByTagName("email")[0].childNodes[0].nodeValue;
			currentUserEmail = email;
			currentUserId=userName;
			$id("profileUserName").innerText = userName.substring(0, 5);
			$id("icon-avatar").src = avatar;
			$id("profilePicture").src = avatar;
			var key = "home";
			addActive(key);
			//sessionStorage.userName=userName.substring(0,5);
		},
		failure : function(xhr) {
			console.log("fail");
			
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
};

function doPleaseLogin() {
	alert("please log in to access");
	$id("panel").removeClass(cssHide);
	$id("btnLogin").addClass("active");
}



function uploadFiles(event) {
	var formData = new FormData();
	var file = event.currentTarget[0].files[0];
	formData.append("file", file);
	formData.append("backendUrl", "demo2");

	var xhr = new XMLHttpRequest();
	xhr.open('POST', "http://localhost:8080/book-service/user/changeAvatar", true);
	xhr.onload = function(e) {
		alert("oke");
	};

	xhr.send(formData);
	// multipart/form-data
	alert("return");
	return false;
}

function addActive(key) {
	var arr = ["home", "books", "about"];
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] == key) {
			$id(key).addClass("active");
			continue;
		}
		$id(arr[i]).removeClass("active");

	}
}

// document.querySelector('input[type="file"]').addEventListener('change', function(e) {
// uploadFiles('/server', this.files);
// }, false);

function formatDateTime(date) {
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return year + "/"
            + (month < 10 ? "0" + month : month) + "/"
            + (day < 10 ? "0" + day : day) + " "
            + (hour < 10 ? "0" + hour : hour) + ":"
            + (minute < 10 ? "0" + minute : minute) + ":"
            + (second < 10 ? "0" + second : second) + ":"
}